<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">床位管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddBed">新增床位</el-button>
  </div>
  <!-- 新增床位 -->
  <!-- :before-close="handleClose" 在弹窗关闭前先调用handleClose方法来处理弹窗的关闭问题
   避免点击弹窗外部也能关闭弹窗-->
  <el-dialog
      :title="dialogTitle"
      style="width: 500px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="床位名称">
            <el-input placeholder="请输入床位名称" v-model="saveBedForm.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="床位类型">
            <el-select placeholder="请选择床位类型" v-model="saveBedForm.type">
              <el-option v-for="item in bedTypeArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="价格">
            <el-input placeholder="请输入价格" v-model="saveBedForm.price"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="配套物品">
            <el-input placeholder="请输入配套物品" v-model="saveBedForm.belongings"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveBed">保存</el-button>
    </template>
  </el-dialog>

  <!-- 床位搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="床位类型" style="width: 220px;">
        <el-input placeholder="输入床位类型" v-model="searchBedForm.type" @keydown.enter="loadBed"></el-input>
      </el-form-item>
      <el-form-item label="价格" style="width: 220px;">
        <el-input placeholder="输入价格" v-model="searchBedForm.price" @keydown.enter="loadBed"></el-input>
      </el-form-item>
      <el-form-item label=床位状态 style="width: 220px;">
        <el-select placeholder="请选择床位状态" v-model="searchBedForm.status" @change="loadBed">
          <el-option label="空闲" value="1"></el-option>
          <el-option label="已占用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadBed">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 床位表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="bedArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="床位名称" align="center"></el-table-column>
      <el-table-column prop="type" label="床位类型" align="center" :formatter="bedTypeFormatter"></el-table-column>
      <el-table-column prop="price" label="床位价格" align="center"></el-table-column>
      <el-table-column prop="belongings" label="配套物品" align="center"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      <el-table-column prop="status" label="床位状态" align="center">
        <template #default="scope">
          <!--  scope.row就代表表格中当前行的床位对象,也就是后端返回的bedVO -->
          <el-switch active-value="1" inactive-value="0"
                     v-model="scope.row.status"
                     @change="changeStatus(scope.row.id,scope.row.status)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editBed(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteBed(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//对入住状态显示内容进行文字转换
const bedTypeFormatter = (row,column,cellValue,index)=>{
  if(!bedTypeArr.value){
    return cellValue;
  }
  for(let item of bedTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新建用户');

//定义对象用来收集保存表单的所有床位数据
const saveBedForm = ref({
  id:'',
  name:'',
  type:'',
  price:'',
  belongings:'',
});
/* 点击"新增床位"按钮,出弹窗+修改弹窗标题 */
const beforeAddBed = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新增床位';
}
//定义保存床位的方法
const saveBed = ()=>{
  console.log(saveBedForm.value);
  /* 安装查询字符串框架qs  npm install qs
  使用qs框架将JS对象转为查询字符串格式 name=tom&age=18,注意qs需要导入!!! import qs from "qs"; */
  let data = qs.stringify(saveBedForm.value);
  console.log(data);
  //向后端发送保存床位的请求
  /* 此处传的参数是qs转换后的查询字符串data,注意:使用了查询字符串,后端Controller不用加@RequestBody注解!!! */
  axios.post(BASE_URL+'/v1/checkIn/bed/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          //关闭弹窗,并清空双向绑定的表单数据
          dialogVisible.value = false;
          saveBedForm.value = {};
          //保存成功后要重新加载床位数据!
          loadBed();
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本窗口吗?')){
    //关闭弹窗,并清空双向绑定的表单数据
    dialogVisible.value = false;
    saveBedForm.value = {};
  }
}

//定义对象用来保存床位的查询条件
const searchBedForm = ref({type:'', price:'',status:''});
//定义对象保存查询到的床位表格数据
const bedArr = ref([]);
//定义加载床位的方法
const loadBed = ()=>{
  let data = qs.stringify(searchBedForm.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有床位数据
  axios.get(BASE_URL+'/v1/checkIn/bed/select?'+data)
      .then((response)=>{//回调函数中处理响应结果
        if(response.data.code == 2000){
          bedArr.value = response.data.data;//将后端响应数据装入床位数组中
          console.log(bedArr.value);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//定义重置搜索条件的方法
const resetSearch = ()=>{
  //清空之前的搜索条件
  searchBedForm.value = {};
  //重新加载所有的床位数据
  loadBed();
}

/* 页面一加载完毕,立即执行 */
onMounted(()=>{
  loadBed();
})

const bedTypeArr=ref([]);
onMounted(()=>{
  loadDictOption(bedTypeArr,'bed_type')
})

//定义编辑床位的方法
const editBed = (id)=>{
  //出弹窗 + 修改弹窗标题
  dialogVisible.value = true;
  dialogTitle.value = '编辑床位';
  //根据床位id查询当前床位的详细数据显示在弹窗表单中
  axios.get(BASE_URL+'/v1/checkIn/bed/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      //注意:后端返回的是List<BedVO>,所以查一条数据的话拿的是集合的第1个元素,data[0]等价list.get(0)
      saveBedForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//定义修改床位状态的方法
const changeStatus = (id,status)=>{
  axios.post(BASE_URL+'/v1/checkIn/bed/update/status/'+id+'/'+status)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('修改状态成功!');
          loadBed();
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

//创建删除床位的方法
const deleteBed = (bedId)=>{
  if(confirm('您确认要删除该床位吗?')){
    axios.post(BASE_URL+'/v1/checkIn/bed/delete/'+bedId)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success('删除成功!');
            loadBed();//重新加载床位数据
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}
</script>

<style scoped>

</style>